package cn.wuxing.mediation.businessplatform.service.impl;

import cn.wuxing.mediation.businessplatform.domain.dto.AllCaseDataPageRequestDTO;
import cn.wuxing.mediation.businessplatform.domain.pojo.AreaVO;
import cn.wuxing.mediation.businessplatform.mapper.business.AllCaseDataMapper;
import cn.wuxing.mediation.businessplatform.mapper.system.AreaMapper;
import cn.wuxing.mediation.businessplatform.service.IAllCaseDataService;
import cn.wuxing.mediation.common.constant.Constants;
import cn.wuxing.mediation.common.core.domain.entity.AllCaseData;
import cn.wuxing.mediation.common.core.domain.model.LoginUser;
import cn.wuxing.mediation.common.core.page.TableDataInfo;
import cn.wuxing.mediation.common.utils.EncryptionUtils;
import cn.wuxing.mediation.common.utils.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * AllCaseData类（all_case_data表）的业务逻辑处理函数定义实现类
 *
 * @author kwainoiiii
 */

@Slf4j
@Service
public class AllCaseDataServiceImpl extends ServiceImpl<AllCaseDataMapper, AllCaseData> implements IAllCaseDataService {
    private final AllCaseDataMapper allCaseDataMapper;
    private final AreaMapper areaMapper;

    public AllCaseDataServiceImpl(AllCaseDataMapper allCaseDataMapper, AreaMapper areaMapper) {
        this.allCaseDataMapper = allCaseDataMapper;
        this.areaMapper = areaMapper;
    }


    @Override
    public TableDataInfo allCaseDataPagination(AllCaseDataPageRequestDTO requestDTO) {
//         TODO 区域数据权限
        Long dataScopeId = SecurityUtils.getDataScope();
        String dataScopeCode = areaMapper.selectCodeById(dataScopeId);
        if (StringUtils.isEmpty(dataScopeCode)) {
            dataScopeCode = Constants.DATA_SCOPE_CODE;
        }
        requestDTO.setAreaCode(dataScopeCode);
        if (StringUtils.isNotBlank(requestDTO.getAreaName())) {
            AreaVO areaByName = areaMapper.getAreaByName(requestDTO.getAreaName());
            log.info("根据区域名称查询到的区域信息为：{}", areaByName);
            if (Objects.nonNull(areaByName)) {
                requestDTO.setAreaId(areaByName.getAreaId());
            }
        }
        List<AllCaseData> records = allCaseDataMapper.selectAllCasePage(requestDTO);
        records = records.parallelStream().peek(
                allCaseData -> {
                    if (StringUtils.isNotBlank(allCaseData.getAreaId())) {
                        allCaseData.setAreaName(areaMapper.selectNameById(Long.valueOf(allCaseData.getAreaId())));
                    }
                    String dsrString = allCaseData.getDsr();
                    if (dsrString != null && !dsrString.isEmpty()) {
                        String[] dsr = dsrString.split(",");
                        if (dsr.length > 0) {
                            List<String> des = new ArrayList<>();
                            try {
                                des = Arrays.stream(dsr).map(EncryptionUtils::decrypt).collect(Collectors.toList());
                            } catch (Exception ex) {
//                                log.error("解密失败", ex);
                            } finally {
                                if (!des.isEmpty()) {
                                    String collect = des.stream().map(str -> EncryptionUtils.mask(str, "**")).collect(Collectors.joining(","));
                                    allCaseData.setDsr(collect);
                                } else {
                                    allCaseData.setDsr(EncryptionUtils.mask(allCaseData.getDsr(), "**"));
                                }
                            }
                        }
                    }
                }).collect(Collectors.toList());
        log.info("数据库查询的offset信息是：{}-{}", requestDTO.getOffsetStart(), requestDTO.getOffsetEnd());
        Integer total = allCaseDataMapper.selectAllCasePageCount(requestDTO);
        log.info("查询到的全量库数据总数为：{}", total);
        return new TableDataInfo(records, total);
    }
}
